package com.cndw.kungfu.vo.goodsvo;

import java.util.HashMap;
import java.util.Map;

import com.cndw.commons.util.BlankUtils;
import com.cndw.kungfu.config.EquipType;
import com.cndw.kungfu.domain.Goods;
import com.cndw.kungfu.domain.GoodsExt;
import com.cndw.kungfu.domain.GoodsMode;
import com.cndw.kungfu.domain.GoodsSuite;
import com.cndw.kungfu.domain.GoodsSuiteProp;
import com.cndw.kungfu.model.Context;
import com.cndw.kungfu.model.goods.domain.ModeGoodsInlay;

/**
 * 物品属性中的装备vo
 * 
 * @author fantadust [9677889@qq.com]
 * @date 2011-6-7 下午02:51:03
 */
public class EquipVO {
	/**
	 * 装备位置
	 */
	public int position;

	/**
	 * 需要力量
	 */
	public int power;
	/**
	 * 需要体力
	 */
	public int physical;
	/**
	 * 需要敏捷
	 */
	public int agile;
	/**
	 * 需要智力
	 */
	public int wit;
	/**
	 * 装备加血
	 */
	public int hp;
	/**
	 * 装备加蓝
	 */
	public int mp;
	/**
	 * 攻击
	 */
	public int attack;
	/**
	 * 防御
	 */
	public int defense;
	/**
	 * 命中率
	 */
	public int hitR;
	/**
	 * 闪避率
	 */
	public int dodgeR;
	/**
	 * 攻速
	 */
	public int speed;
	/**
	 * 幸运
	 */
	public boolean luck;
	/**
	 * 锻造等级
	 */
	public int forgeLevel;
	/**
	 * 打孔数
	 */
	public int holeCounts;
	/**
	 * 宝石属性
	 */
	public String gem;// 宝石属性

	/**
	 * 镶嵌宝石
	 */
	public Map<Integer, GoodsVO> gemMap = new HashMap<Integer, GoodsVO>();

	/**
	 * 宝石追加属性
	 */
	public Map<Integer, ModeGoodsInlay> gemAppend = new HashMap<Integer, ModeGoodsInlay>();

	public int classId;

	/**
	 * 装备评分
	 */
	public int score;

	/**
	 * 套装
	 */
	public GoodsSuiteVO suiteVO;

	/**
	 * 星级奖励追加命中
	 */
	public int appendHit;
	/**
	 * 星级奖励追加闪避
	 */
	public int appendDodge;
	/**
	 * 星级奖励追加暴击
	 */
	public int appendCrit;
	/**
	 * 星级奖励追加韧性
	 */
	public int appendTough;
	/**
	 * 锻造直接值
	 */
	public int forgeAppend;
	
	public EquipVO(){
		
	}

	/**
	 * 从背包里面初始化装备属性
	 * 
	 * @param goods
	 * @param goodsMode
	 */
	public EquipVO(Goods goods, GoodsMode goodsMode) {

		position = goods.getPosition();

		hp = goods.getHp();
		mp = goods.getMp();
		attack = goods.getAttack();
		defense = goods.getDefense();
		hitR = goods.getHitR();
		dodgeR = goods.getDodgeR();
		forgeLevel = goods.getForgeLevel();
		holeCounts = goods.getHoleCounts();
		gem = goods.getGem();
		luck = goods.isLuck();

		int level = goods.getLevel();

		// 锻造追加值
		forgeAppend = goods.getForgeLevel() * goods.getGoodsMode().getAppend();
		if (goods.getForgeLevel() <= 20) {
			forgeAppend = goods.getForgeLevel() * goods.getGoodsMode().getAppend();
		} else {
			forgeAppend = goods.getGoodsMode().getAppend() * 20 + (goods.getGoodsMode().getAppend() * (goods.getForgeLevel() - 20)) / 2;
		}

		// 装备镶嵌宝石
		if (!BlankUtils.isBlank(goods.getGemMap())) {
			for (Integer hole : goods.getGemMap().keySet()) {
				int id = goods.getGemMap().get(hole);
				if (id > 0) {
					gemAppend.put(hole, ModeGoodsInlay.get(id));
					gemMap.put(hole, Context.getGoodsServer().getModeById(id).toGoodsVO());
				}
			}
		}

		initCommValue(goods.getPlayerId(), goodsMode, level);
	}

	/**
	 * 从商城或者其他地方初始化装备属性
	 * 
	 * @param level
	 * @param goodsMode
	 */
	public EquipVO(int level, long playerId, GoodsMode goodsMode, boolean isLuck) {
		int goodsId = goodsMode.getGoodsId();
		GoodsExt goodsExt = Context.getCacheCenter().getGoodsExtByLevel(goodsId, level);
		if (goodsExt != null) {
			hp = goodsExt.getHp();
			mp = goodsExt.getMp();
			attack = goodsExt.getAttack();
			defense = goodsExt.getDefense();
		}
		luck = isLuck;

		initCommValue(playerId, goodsMode, level);

	}

	private void initCommValue(long playerId, GoodsMode goodsMode, int level) {
		classId = goodsMode.getClassId();
		speed = goodsMode.getSpeed();
		power = goodsMode.getPower();
		physical = goodsMode.getPhysical();
		agile = goodsMode.getAgile();
		wit = goodsMode.getWit();

		// 套装属性
		if (classId > 0 && playerId > 0) {

			// 获取身上穿的装备所属的所有套装
			Map<Integer, GoodsSuiteVO> sutieVOMap = Context.getGoodsServer().getGoodsSuites(playerId);

			if (sutieVOMap.containsKey(classId)) {
				suiteVO = sutieVOMap.get(classId);
			} else {
				GoodsSuite suite = Context.getCacheCenter().getGoodsSuiteById(goodsMode.getClassId());
				if (suite != null) {
					suiteVO = new GoodsSuiteVO();
					suiteVO.setId(suite.getId());
					suiteVO.setName(suite.getName());

					for (Integer goodsId : suite.getGoods().keySet()) {
						GoodsMode mode = Context.getGoodsServer().getModeById(goodsId);
						suiteVO.getGoods().put(goodsId, new Object[] { mode.getName(), false });
					}

					for (GoodsSuiteProp prop : suite.getProps()) {
						GoodsSuitePropVO propVO = prop.toGoodsSuitePropVO();
						propVO.setHasCounts(0);
						suiteVO.getProps().put(prop.getConditions(), propVO);
					}
				}

			}
		}

		// 星级追加属性
		int[] append = Context.getGoodsServer().calculateGoodsStar(goodsMode, level);
		appendHit += append[0];
		appendDodge += append[1];
		appendCrit += append[2];
		appendTough += append[3];

		// 装备评分
		calculateScore();

		if (position == 0) {
			EquipType equipType = EquipType.convert2Type(goodsMode.getGoodTypeId());
			if (equipType != null) {
				position = equipType.position();
			}
		}
	}

	/**
	 * 装备评分计算
	 */
	private void calculateScore() {
		int eHp = hp, eMp = mp, eAttack = attack, eDefense = defense, eHit = hitR, eDodge = dodgeR, eCrit = 0, eTough = 0;

		// 加上锻造追加属性
		if (eAttack > 0) {
			eAttack += forgeAppend;
		}
		if (eHp > 0) {
			eHp += forgeAppend;
		}
		if (eMp > 0) {
			eMp += forgeAppend;
		}
		if (eDefense > 0) {
			eDefense += forgeAppend;
		}

		// 加上星级追加值
		eHit += appendHit;
		eDodge += appendDodge;
		eCrit += appendCrit;
		eTough += appendTough;

		// 加上宝石镶嵌值
		if (!gemAppend.isEmpty()) {
			for (ModeGoodsInlay inlay : gemAppend.values()) {
				eHp += inlay.getHp();
				eMp += inlay.getMp();
				eAttack += inlay.getAttack();
				eDefense += inlay.getDefense();
				// eSpeed += inlay.getSpeed();
			}
		}

		score = (int) ((eHp + eMp) + eAttack * 3 + eDefense * 2 + (eHit + eDodge + eCrit + eTough + speed) * 5 + (luck ? 500 : 0));
		score = score <= 0 ? 1 : score;
	}

}
